package com.wjb.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wjb.springcloud.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class StudentConsumerController {

    @Autowired
    private RestTemplate restTemplate;

//    @Autowired
//    private LoadBalancerClient loadBalancerClient;
    @HystrixCommand(fallbackMethod = "findByIdFallback")
//    ,commandProperties = @HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE`")
    @RequestMapping(value = "/movie/{id}",method = RequestMethod.GET)
    public Student findById(@PathVariable("id") Long id){
       return restTemplate.getForObject("http://user-service/simple/"+id,Student.class);
    }
    public Student findByIdFallback(Long id){
        Student s=new Student();
        s.setId(0L);
        return s;
    }
//    @RequestMapping(value = "/test",method = RequestMethod.GET)
//    public void test(){
//        ServiceInstance serviceInstance=loadBalancerClient.choose("user-service");
//        System.out.println("---------------第一个服务:"+serviceInstance.getServiceId()+"主机名："+serviceInstance.getHost()+"端口号:"+serviceInstance.getPort());
//        ServiceInstance serviceInstance2=loadBalancerClient.choose("user-service2");
//        System.out.println("-------第二个服务:"+serviceInstance2.getServiceId()+"主机名："+serviceInstance2.getHost()+"端口号:"+serviceInstance2.getPort());
//    }
}
